package com.example.check_oa.utils;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.example.comment_oa.pojos.MouthAt;
import com.example.comment_oa.pojos.attendance.MouthAtVo;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class GetAntten {
//    @Resource
//    RedisTemplate redisTemplate;

//    @Autowired
//    public void setRedisTemplate(RedisTemplate redisTemplate) {
//        GetAntten.redisTemplate = redisTemplate;
//    }

//    @Test
//    public void d(){
//        System.out.println(redisTemplate);
//    }

    public static void main(String[] args) {
//        Date gdtDate=DateUtil.parse("17:00:00");
//        String day="2022-10-28 16:47:55";
//        System.out.println(day.substring(11, 19));
//        Date dk = DateUtil.parse(day.substring(11, 19));
//        long second=DateUtil.between(gdtDate,dk,DateUnit.SECOND);
//        System.out.println(second);
//        List<String> arrayList = Arrays.asList(holiday2);
//        List<String> stringList = queryMonthDay("2023-10-01",new ArrayList());
//        stringList.forEach(e->{
//            System.out.println(e);
//        });
        GetAntten.threeMouth().forEach(e->{
            System.out.println(e);
        });
//        stringList.addAll(arrayList);
//        stringList.forEach(e-> System.out.println(e));
//        System.out.println(getYearWorkDay("2022").size());
//        System.out.println(GetAntten.threeMouth());
    }


    /**
     * 字符串转时间
     *
     * @param dateStr
     * @param index
     * @return
     */
    public static Date StringToDate(String dateStr, int index) {
        DateFormat df = null;
        try {
            df = new SimpleDateFormat(dateFormat[index]);
            return df.parse(dateStr);
        } catch (Exception aioe) {
            return null;
        }
    }

    /**
     * 定义常见的时间格式
     */
    private static String[] dateFormat = {
            "yyyy-MM-dd HH:mm:ss", // 0
            "yyyy/MM/dd HH:mm:ss", // 1
            "yyyy年MM月dd日HH时mm分ss秒", // 2
            "yyyy-MM-dd", // 3
            "yyyy/MM/dd", // 4
            "yy-MM-dd", // 5
            "yy/MM/dd", // 6
            "yyyy年MM月dd日", // 7
            "HH:mm:ss", // 8
            "yyyyMMddHHmmss", // 9
            "yyyyMMdd", // 10
            "yyyy.MM.dd", // 11
            "yy.MM.dd", // 12
            "MM月dd日HH时mm分", // 13
            "yyyy年MM月dd日 HH:mm:ss", // 14
            "yyyy-MM-dd HH:mm", // 15
            "yyMMdd" // 16
    };

    /**
     * 获取法定节假日或者调休
     *
     * @param num
     * @return
     */
    public static List<String> holiday(int num) {

        if (num == 2) {
            return Arrays.asList(holiday2);
        } else {
            return Arrays.asList(holiday1);
        }

    }


    /**
     * 去重
     *
     * @param str
     * @return
     */
    public static List<String> removal(List<String> str) {
        Set<String> s = new HashSet<String>(str);
        str.clear();
        str.addAll(s);
        return str;
    }

    // 假期
    public static String[] holiday1 = {
            "2023-01-01", // 元旦
            "2023-01-02", // 元旦
            "2023-01-21", // 春节
            "2023-01-22", // 春节
            "2023-01-23", // 春节
            "2023-01-24", // 春节
            "2023-01-25", // 春节
            "2023-01-26", // 春节
            "2023-01-27", // 春节
            "2023-04-05", // 清明节
            "2023-04-29", // 劳动节
            "2023-04-30", // 劳动节
            "2023-05-01", // 劳动节
            "2023-05-02", // 劳动节
            "2023-05-03", // 劳动节
            "2023-06-22", // 端午节
            "2023-06-23", // 端午节
            "2023-06-24", // 端午节
            "2023-09-29", // 中秋节
            "2023-09-30", // 中秋节
            "2023-10-01", // 国庆节
            "2023-10-02", // 国庆节
            "2023-10-03", // 国庆节
            "2023-10-04", // 国庆节
            "2023-10-05", // 国庆节
            "2023-10-06", // 国庆节
    };

    /**
     * 查询每个月的工作时间(天)
     *
     * @return
     */
    public static List<String> queryMonthDay(String day, List day2) {
        List<String> result = new ArrayList<String>();
        Calendar Day = Calendar.getInstance();
        Day.setTime(StringToDate(day, 3));
        // 获取该月天数
        int dayNum = Day.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 月开始
        String st = day.substring(0, 8) + "01";
        // 月结束
        String en = day.substring(0, 8) + dayNum;
        // 月份数据集合
        for (int i = 1; i < dayNum + 1; i++) {
            String d = day.substring(0, 8) + ((i < 10) ? "0" + i : "" + i);
            // 去掉周末
            Calendar startDay = Calendar.getInstance();
            startDay.setTime(StringToDate(d, 3));
            int week = startDay.get(Calendar.DAY_OF_WEEK);
            if (7 != week && 1 != week) {
                result.add(d);
            }
        }
        // 获取法定节假日
        List<String> fdList = holiday(1);
        // 添加时间段中间应该上班的时间
        // 去除中间所有的法定假期
        result.removeAll(fdList);
        // 去重
        result = removal(result);
        result.addAll(day2);
        return result;
    }

    // 调休
    public static String[] holiday2 = {
            "2023-01-28", // 春节_调休
            "2023-01-29", // 春节_调休
            "2023-04-23", // 劳动_节调休
            "2023-05-06", // 劳动_节调休
            "2023-06-25",//端午节调休
            "2023-10-07", // 国庆节_调休
            "2023-10-08", // 国庆节_调休
    };
    //获取当月调休集合
    public static List<String> get(String day){
        List<String> d=new ArrayList();
        d.add("2023-01-28");
        d.add("2023-01-29");
        d.add("2023-04-23");
        d.add("2023-05-06");
        d.add("2023-06-25");
        d.add("2023-10-07");
        d.add("2023-10-08");
        List<String> b=new ArrayList();
        for (int i = 0; i < d.size(); i++) {
            if(d.get(i).indexOf(day)==0){
                b.add(d.get(i));
            }
        }
        return b;
    }

    //今年所有调休
    public static List<String> getAllTx(){
        List<String> d=new ArrayList();
        d.add("2023-01-28");
        d.add("2023-01-29");
        d.add("2023-04-23");
        d.add("2023-05-06");
        d.add("2023-06-25");
        d.add("2023-10-07");
        d.add("2023-10-08");
        return d;
    }

    //判断时间是否在范围之内
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    //日期经过日期
    public static List<String> getBetweenDate(String startTime, String endTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime()<=endDate.getTime()){
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate=calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }


    //求两个list集合交集
    public static List<String> intersectList2(List<String> list1, List<String> list2) {
        Map<String, String> tempMap = list2.parallelStream().collect(Collectors.toMap(Function.identity(), Function.identity(), (oldData, newData) -> newData));
        return list1.parallelStream().filter(str -> {
            return tempMap.containsKey(str);
        }).collect(Collectors.toList());
    }
    //list集合差集
    public static List<String> subList2(List<String> list1, List<String> list2) {
        Map<String, String> tempMap = list2.parallelStream().collect(Collectors.toMap(Function.identity(), Function.identity(), (oldData, newData) -> newData));
        return list1.parallelStream().filter(str -> {
            return !tempMap.containsKey(str);
        }).collect(Collectors.toList());
    }
    //三天前集合
    public static List threeDay(int day){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List list=new ArrayList();
        //过去3天
        for (int i = 1; i <= day; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE,-i);
            Date date = calendar.getTime();
            String formatDay = dateFormat.format(date);
            list.add(formatDay);
        }
        return list;
    }

    //工作日，没加调休
    public static List<String> queryMonthDay2(String day) {
        List<String> result = new ArrayList<String>();
        Calendar Day = Calendar.getInstance();
        Day.setTime(StringToDate(day, 3));
        // 获取该月天数
        int dayNum = Day.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 月开始
        String st = day.substring(0, 8) + "01";
        // 月结束
        String en = day.substring(0, 8) + dayNum;
        // 月份数据集合
        for (int i = 1; i < dayNum + 1; i++) {
            String d = day.substring(0, 8) + ((i < 10) ? "0" + i : "" + i);
            // 去掉周末
            Calendar startDay = Calendar.getInstance();
            startDay.setTime(StringToDate(d, 3));
            int week = startDay.get(Calendar.DAY_OF_WEEK);
            if (7 != week && 1 != week) {
                result.add(d);
            }
        }
        // 获取法定节假日
        List<String> fdList = holiday(1);
        // 添加时间段中间应该上班的时间
        // 去除中间所有的法定假期
        result.removeAll(fdList);
        // 去重
        result = removal(result);
        return result;
    }

    //获取月份
    public static String[] getYearFullMonth(String year) {
        String[] array = new String[12];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(year));
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DATE,1);
        for (int i = 0; i < 12; i++) {
            array[i] = sdf.format(cal.getTime());
            cal.add(Calendar.MONTH, 1);
        }
        return array;
    }

    //一年的工作日
    public static List<String> getYearWorkDay(){
        int year = DateUtil.year(DateUtil.date());
        String[] yearFullMonth = getYearFullMonth(String.valueOf(year));
        List<String> mothList=Arrays.asList(yearFullMonth);
        List<String> day=new ArrayList<>();
        mothList.forEach(e-> {
            day.addAll(queryMonthDay2(e));
        });
        List<String> arrayList = Arrays.asList(holiday2);
        day.addAll(arrayList);
        return day;
    }

    //三个月
    public static List<String> threeMouth(){
        List<String> ii=new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//格式化为2017-10
        Calendar calendar = Calendar.getInstance();//得到Calendar实例
        calendar.add(Calendar.MONTH, -3);//把月份减三个月
        Date starDate = calendar.getTime();//得到时间赋给Data
        String stardtr = formatter.format(starDate);//使用格式化Data
        calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH, -2);//把月份减三个月
        Date starDate1 = calendar.getTime();//得到时间赋给Data
        String stardtr1 = formatter.format(starDate1);//使用格式化Data
        calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);//把月份减三个月
        Date starDate2 = calendar.getTime();//得到时间赋给Data
        String stardtr2 = formatter.format(starDate2);//使用格式化Data
        ii.add(stardtr2);
        ii.add(stardtr1);
        ii.add(stardtr);
        return ii;
    }

    public static void write(Object o,List list){
        Class<?> aClass = o.getClass();
        System.out.println(aClass.getClass());
        //工作铺对象
        ExcelWriterBuilder write = EasyExcel.write("D:\\study\\test05.xlsx", MouthAtVo.class);
        //工作表对象
        ExcelWriterSheetBuilder sheet = write.sheet();
        //准备数据
        list.forEach(e->{
            System.out.println(e);
        });
        //写
        sheet.doWrite(list);
    }

}


